//
//  JLLeftDockMenu.m
//  LIangeNews
//
//  Created by sun on 16/1/30.
//  Copyright © 2016年 sun. All rights reserved.
//

#import "JLLeftDockMenu.h"
#import "JLLeftMenuButton.h"
#import "UIView+Frame.h"
@implementation JLLeftDockMenu

-(instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        [self setupChildBtnWithTitle:@"新闻" imageStr:@"sidebar_nav_news"];
        [self setupChildBtnWithTitle:@"订阅" imageStr:@"sidebar_nav_reading"];
        [self setupChildBtnWithTitle:@"图片" imageStr:@"sidebar_nav_photo"];
        [self setupChildBtnWithTitle:@"视频" imageStr:@"sidebar_nav_comment"];
        [self setupChildBtnWithTitle:@"电台" imageStr:@"sidebar_nav_radio"];
        
    }
    return self;
}


-(void)setupChildBtnWithTitle:(NSString *)title imageStr:(NSString *)imageStr{
    
    JLLeftMenuButton * btn = [[JLLeftMenuButton alloc]init];
    btn.tag = self.subviews.count;
    [self addSubview:btn];
    [btn setTitle:title forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(childBtnClick:) forControlEvents:UIControlEventTouchDown];
    btn.adjustsImageWhenHighlighted = NO;
    btn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    [btn setImage:[UIImage imageNamed:imageStr] forState:UIControlStateNormal];
    
    btn.backgroundColor = JLRandomColor;
    btn.titleLabel.font = [UIFont systemFontOfSize:17];
    btn.contentEdgeInsets = UIEdgeInsetsMake(0, 30, 0, 0);
    btn.titleEdgeInsets = UIEdgeInsetsMake(0, 30, 0, 0);

    
}
-(void)layoutSubviews{
    NSUInteger count = self.subviews.count;
    CGFloat width = self.width;
    CGFloat height = self.height / count;
    for (int i = 0; i < count ; i++) {
        JLLeftMenuButton * btn = self.subviews[i];
        CGFloat y = i * height;
        btn.frame = CGRectMake(0, y, width, height);
    }
    
}
-(void)childBtnClick:(JLLeftMenuButton *)btn{
    JLLog(@"第%lu个菜单按钮%@被点击",btn.tag,btn.titleLabel.text);
    [JLNoteCenter postNotificationName:JLLeftMenuButtonDidSelectedNotification object:btn];
    
}
@end
